package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.*;
import com.it.tydic.estate.module.mapper.inner.TbFamilyMapper;
import com.it.tydic.estate.module.model.inner.TbFamily;
import com.it.tydic.estate.module.service.TbFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: liuhui
 * @createDate: 2020/3/5
 * @version: 1.0
 */
@Service
@Transactional
public class TbFamilyServiceImpl implements TbFamilyService {
    @Autowired
    private TbFamilyMapper familyMapper;    /*家庭成员*/

    /**
     * 家庭成员列表
     * @param map
     * @return
     */
    @Override
    public Result selectFamilyList(Map<String, Object> map) {
        /*分页*/
//        if(map.containsKey("pageNum") && map.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
//        }
        CommonUtils.pageProcess(map);//验证处理分页
        Page<TbFamily> tbFamilies = familyMapper.selectFamilyList(map);
        List<TbFamily> result = tbFamilies.getResult();
        if(result.size()>0 && result!= null ){
            /*码表赋值*/
            for (TbFamily tbFamily : result) {
                tbFamily.setRelationship(DictionaryCache.getDicVal(tbFamily.getRelationship()));
                tbFamily.setPoliticalStatus(DictionaryCache.getDicVal(tbFamily.getPoliticalStatus()));
                tbFamily.setMaritalStatus(DictionaryCache.getDicVal(tbFamily.getMaritalStatus()));
                tbFamily.setAccountTypes(DictionaryCache.getDicVal(tbFamily.getAccountTypes()));
            }
        }
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("result",result);
        resultMap.put("total",tbFamilies.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 成员详情
     * @param uid
     * @return
     */
    @Override
    public Result selectFamilyById(String uid) {
        /*参数验证*/
        if (StringUtils.isNotEmpty(uid)){
            return Result.success(familyMapper.selectByPrimaryKey(uid));
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

    /**
     * 家庭成员添加修改
     * @param tbFamily
     * @return
     */
    @Override
    public Result addFamily(TbFamily tbFamily) {
        int num = 0;
        tbFamily.setZfbj(ZfbjFinalUtil.zfbj_zy);
        if(StringUtils.isNotEmpty(tbFamily.getUid())){      /*修改*/
            tbFamily.setXgsj(new Date());
            /*创建修改人*/
            tbFamily.setUuserId(CurrentUtil.getCurrentUserId());
            num = familyMapper.updateByPrimaryKeySelective(tbFamily);
            return num >0 ? Result.success(tbFamily.getUid()):Result.error(CodeMsg.UPDATE_ERROR);
        }else{      /*添加*/
            tbFamily.setCjsj(new Date());
            /*创建修改人*/
            tbFamily.setUuserId(CurrentUtil.getCurrentUserId());
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());//生成Id主键
            tbFamily.setUid(String.valueOf(id));
            num = familyMapper.insertSelective(tbFamily);
        }
        return num >0 ? Result.success(tbFamily.getUid()):Result.error(CodeMsg.INSERT_ERROR);
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @Override
    public Result delFamily(List<String> list) {
        /*参数验证*/
        if(list.size()<0 || list == null)
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        return familyMapper.deleteByList(list) >0 ? Result.success():Result.error(CodeMsg.DELETE_ERROR);
    }

}
